﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command;
using S.Archives.Domain.S.ArchivesModel;
using S.Archives.Infrstructre.InterFace;

namespace S.Archives.Api.Application.CommandHandlers
{
    /// <summary>
    /// 修改羊只档案
    /// </summary>
    public class SheepFileUpdateCommandHandler : IRequestHandler<SheepFileUpdateCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public SheepFileUpdateCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }
        public Task<bool> Handle(SheepFileUpdateCommand request, CancellationToken cancellationToken)
        {
            var sheep= sheepFileRepository.GetAll().FirstOrDefault(x=>x.SheepFileId==request.SheepFileId);
            sheep.MotherEarTag=request.MotherEarTag;
            sheep.ParentEarTag=request.ParentEarTag;
            sheep.Variety = request.Variety;
            if (request.FileType == 3)
            {
                sheep.Sex = request.Sex;
                sheep.BirthState = request.BirthState;
                sheep.GeneRank = request.GeneRank;

            }
         
            sheep.BirthDate = request.BirthDate;
            sheep.AdmissionDate= request.AdmissionDate;
            sheep.BirthPlace = request.BirthPlace;
            sheep.BirthWeight = request.BirthWeight;
           
            sheep.Remarks = request.Remarks;
            sheep.UpdateDate = DateTime.Now;
            sheep.UpdateBy = request.UpdateBy;
            //SheepFileModel sheepFileModel = mapper.Map<SheepFileModel>(request);
            return sheepFileRepository.UpdateAsync(sheep);
        }
    }
}
